package com.kitty.mybatis.mybatisv1.executor;

import com.kitty.mybatis.mybatisv1.mapping.KittyMappedStatement;
import com.kitty.mybatis.mybatisv1.mapping.KittyBoundSql;
import com.kitty.mybatis.mybatisv1.session.KittyConfiguration;

import java.sql.SQLException;
import java.util.List;

/**
 * @Description
 * @Author SHEN
 * @Date 2020/11/1
 */
public abstract class KittyBaseExecutor implements Executor {

    protected Executor wrapper;

    protected KittyConfiguration configuration;


    protected KittyBaseExecutor(KittyConfiguration configuration){
        this.configuration = configuration;
        this.wrapper = this;

    }


    @Override
    public <E> List<E> query(KittyMappedStatement ms, Object parameter) throws SQLException {
        //得到绑定sql
        KittyBoundSql boundSql = ms.getBoundSql(parameter);
        //查询
        return query(ms, parameter, boundSql);
    }

    public <E> List<E> query(KittyMappedStatement ms, Object parameter, KittyBoundSql boundSql) throws SQLException {
        List<E> list = queryFromDatabase(ms, parameter, boundSql);
        return list;
    }

    //从数据库查
    private <E> List<E> queryFromDatabase(KittyMappedStatement ms, Object parameter, KittyBoundSql boundSql) throws SQLException {
        List<E> list;
        list = doQuery(ms, parameter, boundSql);
        return list;
    }

    //query-->queryFromDatabase-->doQuery
    protected abstract <E> List<E> doQuery(KittyMappedStatement ms, Object parameter, KittyBoundSql boundSql)
            throws SQLException;
}



